// pages/car/car.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      statusX:0,
      slide:null,
      toggleChecked:false,
      total:0,
      goodsNum:0,
      goodsList:[
        // {
        //   id:1,
        //   name:"小米手机",
        //   img:"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d9d7f31258a1f84a277918cecab14aaa.jpg?f=webp&w=516&h=420",
        //   price:"2999",
        //   priceOld:"3999",
        //   num:1,
        //   checked:false
        // },
        // {
        //   id:2,
        //   name:"小米手机2",
        //   img:"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d9d7f31258a1f84a277918cecab14aaa.jpg?f=webp&w=516&h=420",
        //   price:"2999",
        //   priceOld:"3999",
        //   num:1,
        //   checked:false
        // },
        // {
        //   id:3,
        //   name:"小米手机3",
        //   img:"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d9d7f31258a1f84a277918cecab14aaa.jpg?f=webp&w=516&h=420",
        //   price:"2999",
        //   priceOld:"3999",
        //   num:1,
        //   checked:false
        // },
      ],
      recom: [
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group002067#t=normal&act=product&page=category_home&pid=16391&bid=4030461.1&bpm=83.247.3645590.2&cdm=&uni=8.874&next=6.64.2.16391&tag=8.16391",
            "path": "16391",
            "type": "product"
          },
          "category_name": "近期上新",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/c173825a4f0984f7c6df72d1ed6aad7c.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/c173825a4f0984f7c6df72d1ed6aad7c.png",
          "product_name": "Buds 4 Pro"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group003067#t=normal&act=product&page=category_home&pid=16181&bid=4016868.1&bpm=83.247.3645590.3&cdm=&uni=8.874&next=6.64.2.16181&tag=8.16181",
            "path": "16181",
            "type": "product"
          },
          "category_name": "近期上新",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/9e325892cb9064fb8da0e6707b901974.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/9e325892cb9064fb8da0e6707b901974.png",
          "product_name": "手环7Pro"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group001068#t=normal&act=product&page=category_home&pid=16584&bid=4058410.1&bpm=83.247.3645590.4&cdm=&uni=8.874&next=6.64.2.16584&tag=8.16584",
            "path": "16584",
            "type": "product"
          },
          "category_name": "近期上新",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/67cd5fddb341f0e0fb0a2295e643b0d8.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/67cd5fddb341f0e0fb0a2295e643b0d8.png",
          "product_name": "Necklace"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group002068#t=normal&act=product&page=category_home&pid=16654&bid=4058411.1&bpm=83.247.3645590.5&cdm=&uni=8.874&next=6.64.2.16654&tag=8.16654",
            "path": "16654",
            "type": "product"
          },
          "category_name": "近期上新",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/aca0fd89e19ddb6b51d6597617b4f790.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/aca0fd89e19ddb6b51d6597617b4f790.png",
          "product_name": "骨传导耳机"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group001070#t=normal&act=product&page=category_home&pid=2213800059&bid=3932475.1&bpm=83.247.3645590.6&cdm=&uni=8.874&next=6.64.7.2213800059&tag=8.2213800059",
            "path": "2213800059",
            "type": "product"
          },
          "category_name": "智能耳机",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/bf7fd8183a79d042cfb9dcb62244311e.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/bf7fd8183a79d042cfb9dcb62244311e.png",
          "product_name": "Buds 3 Pro"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group002070#t=normal&act=product&page=category_home&pid=2214900032&bid=3147071.1&bpm=83.247.3645590.7&cdm=&uni=8.874&next=6.64.7.2214900032&tag=8.2214900032",
            "path": "2214900032",
            "type": "product"
          },
          "category_name": "智能耳机",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/8f028192bbb207badeb7dd503e0022bf.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/8f028192bbb207badeb7dd503e0022bf.png",
          "product_name": "Buds 3"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group003070#t=normal&act=product&page=category_home&pid=2221500063&bid=4001105.1&bpm=83.247.3645590.8&cdm=&uni=8.874&next=6.64.7.2221500063&tag=8.2221500063",
            "path": "2221500063",
            "type": "product"
          },
          "category_name": "智能耳机",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/03b4bf93894a1297a7f1c74d46446fac.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/03b4bf93894a1297a7f1c74d46446fac.png",
          "product_name": "Buds 4 Pro"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group001071#t=normal&act=product&page=category_home&pid=2221500117&bid=4001106.1&bpm=83.247.3645590.9&cdm=&uni=8.874&next=6.64.7.2221500117&tag=8.2221500117",
            "path": "2221500117",
            "type": "product"
          },
          "category_name": "智能耳机",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/fb27e27e6587254da27de5ba9c889512.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/fb27e27e6587254da27de5ba9c889512.png",
          "product_name": "Redmi Buds 4"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group002071#t=normal&act=product&page=category_home&pid=14903&bid=3927130.1&bpm=83.247.3645590.10&cdm=&uni=8.874&next=6.64.2.14903&tag=8.14903",
            "path": "14903",
            "type": "product"
          },
          "category_name": "智能耳机",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/564a13ea259b9694941ee897a918b5bd.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/564a13ea259b9694941ee897a918b5bd.png",
          "product_name": "Redmi Buds 3"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group003071#t=normal&act=product&page=category_home&pid=15217&bid=3946816.1&bpm=83.247.3645590.11&cdm=&uni=8.874&next=6.64.2.15217&tag=8.15217",
            "path": "15217",
            "type": "product"
          },
          "category_name": "智能耳机",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/eb09eaca98ea6e8e4273f6d5ebb17017.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/eb09eaca98ea6e8e4273f6d5ebb17017.png",
          "product_name": "红米 Buds 3 青春版"
        },
        {
          "action": {
            "log_code": "31wapcategory_homecategory_group001072#t=normal&act=product&page=category_home&pid=16535&bid=3927134.1&bpm=83.247.3645590.12&cdm=&uni=8.874&next=6.64.2.16535&tag=8.16535",
            "path": "16535",
            "type": "product"
          },
          "category_name": "智能耳机",
          "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/3890a888b96989426d86dd5a4bee317d.png",
          "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/3890a888b96989426d86dd5a4bee317d.png",
          "product_name": "小米Air 2 SE"
        },
      ],


  },
  // 跳转页面
  goPay(){
      // 判断是否登录
      let login = wx.getStorageSync('userInfo');
      if(!login){
        wx.showToast({
          title: '登录后操作!',
          icon:"none",
          duration: 2000
        })
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/my/my',
          })
        },2000)
        return;
      }


      // 判断是否有数据
      let dataLength = this.data.goodsList.filter(item=>item.checked);
      console.log(dataLength.length);
      if(dataLength.length>=1){//加入购物车数量
          wx.navigateTo({
            url: '/pages/pay/pay',
          })
      }else{
        wx.showToast({
          title: '请选择商品。',
          icon:"none",
          duration: 2000
        })
      }
  },

  // 滑动效果
  touchstart(e){
    // console.log(e);
    // console.log(this.data.statusX);
    this.data.statusX = e.changedTouches[0].clientX;//开始位置
  },
  touchend(e){
    console.log(e.target.dataset.id);
    let clientX = e.changedTouches[0].clientX;//结束位置
    if(this.data.statusX - clientX >0){
      console.log("触发滑动效果");
      this.setData({
        slide:e.target.dataset.id
      })
    }else{
      console.log("不触发滑动效果");
      this.setData({
        slide:null
      })
    }
  },
  // 删除数据
  del(e){
    let id = e.target.dataset.id;
    let index = this.data.goodsList.findIndex(item =>item.id == id);//下标
    this.data.goodsList.splice(index,1);//删除静态数据，不会更新页面
    this.setData(this.data);
    this.totalData();//计算总价格
  },
  // 数量加一
  add(e){
    let id = e.target.dataset.id;
    let index = this.data.goodsList.findIndex(item =>item.id == id);//下标
    // 第一种写法
    // let num = this.data.goodsList[index].num + 1;
    // this.data.goodsList[index].num = num;//修改数据
    // this.setData(this.data);
    // 第二种写法
    this.setData({
      ["goodsList["+index+"].num"]:this.data.goodsList[index].num + 1,
      slide:null
    })
    this.totalData();//计算总价格
  },
  // 数量减一
  sub(e){
    let id = e.target.dataset.id;
    let index = this.data.goodsList.findIndex(item =>item.id == id);//下标
    let num = this.data.goodsList[index].num-1;
    this.setData({
      ["goodsList["+index+"].num"]: (num < 1? 1:num),
      slide:null
    })
    this.totalData();//计算总价格
  },
  // 单选功能
  checkboxClick(e){
    let id = e.target.dataset.id;
    let index = this.data.goodsList.findIndex(item =>item.id == id);//下标
    this.setData({
      ["goodsList["+index+"].checked"]: !this.data.goodsList[index].checked,
      slide:null
    })
    this.totalData();//计算总价格

    // 重新计算全框
    this.idToggleBox();

  },
  // 全选功能
  togglebox(){
    let checked = !this.data.toggleChecked;
    this.data.goodsList.forEach(item=>{
      item.checked = checked
    })

    this.setData({
      "goodsList":this.data.goodsList,
      "toggleChecked": checked,
      slide:null
    })
    this.totalData();//计算总价格
  },
  // 判断是否全选
  idToggleBox(){
    let checked = this.data.goodsList.every(item =>item.checked);//只要一个没有选中false
    this.setData({
      "toggleChecked": checked,
      slide:null
    })
  },
  // 计算总价格
  totalData(){
    let num = 0;
    let data = this.data.goodsList.reduce((total,item)=>{
      if(item.checked){
        num++;
        return total += item.price * item.num;
      }else{
        return total
      }
    },0)
    this.setData({
      total:data,
      goodsNum:num
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取缓存数据
    let goodsCarList = wx.getStorageSync('goodsCarList');
    this.setData({
      goodsList:goodsCarList
    })
    this.idToggleBox(); //全选按钮
    this.totalData(); //总数量
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    wx.setStorageSync('goodsCarList', this.data.goodsList)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})